package cn.jbolt.ai.agent.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.jbolt.ai.agent.dao.BaseKeysInfoMapper;
import cn.jbolt.ai.agent.entity.BaseKeysInfo;
import cn.jbolt.ai.agent.service.BaseKeysInfoService;
import cn.jbolt.ai.app.entity.AiApp;
import cn.jbolt.ai.app.service.AiAppNodeService;
import cn.jbolt.ai.core.chain.ChainCenter;
import cn.jbolt.util.Pager;
import cn.jbolt.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BaseKeysInfoServiceImpl extends ServiceImpl<BaseKeysInfoMapper, BaseKeysInfo> implements BaseKeysInfoService {

    private static final Logger logger = LoggerFactory.getLogger(BaseKeysInfoServiceImpl.class);

    @Transactional
    public Result add(BaseKeysInfo baseKeysInfo) {

        boolean save = this.save(baseKeysInfo);
        if (!save) {
            return Result.error("添加失败");
        }
        return Result.success();
    }

    @Override
    public Result update(Long id,BaseKeysInfo baseKeysInfo) {
//        BaseKeysInfo config = this.getOne(new QueryWrapper<BaseKeysInfo>().eq("id", id));
        baseKeysInfo.setId(id);
        boolean save = this.save(baseKeysInfo);
        if (!save) {
            return Result.error("添加失败");
        }
        return Result.success();
//        return Result.success(JSONUtil.toJsonObject(configValue));

    }

    @Override
    public Result delete(Long id) {
        boolean deleted = this.removeById(id);
        if (deleted) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    @Override
    public Result<Pager> listByPage(Integer pageNumber, Integer pageSize, String keywords) {
        QueryWrapper<BaseKeysInfo> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keywords))
            queryWrapper.and(wrapper -> wrapper.like("comp_name", keywords).or().like("comp_scale", keywords)).or().like("keys_type",keywords);
        queryWrapper.orderByDesc("id");
        Page<BaseKeysInfo> pager = new Page<>(pageNumber, pageSize);
        return Result.success(Pager.from(this.page(pager, queryWrapper)));
    }

}
